package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.xuanwei.core.entity.vo.*;
import com.xuanwei.core.enums.AdPositionType;
import com.xuanwei.core.enums.NavigatePosition;
import com.xuanwei.core.service.*;
import com.xuanwei.dao.entity.*;
import com.xuanwei.xwcommon.entity.dto.SimpleProductDto;
import com.xuanwei.xwcommon.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomePageServiceImpl implements HomePageService {

    private IXwBannerService bannerService;

    private IXwNoticeService noticeService;

    private IXwAdManagerService adManagerService;

    private IXwHomepageNavigateService homepageNavigateService;

    private IXwNavigateProductService navigateProductService;

    private IXwCategoryService categoryService;

    private IXwProductService productService;

    public HomePageServiceImpl(IXwBannerService bannerService,
                               IXwNoticeService noticeService,
                               IXwAdManagerService adManagerService,
                               IXwNavigateProductService navigateProductService,
                               IXwCategoryService categoryService,
                               IXwHomepageNavigateService homepageNavigateService,
                               IXwProductService productServic) {
        this.bannerService = bannerService;
        this.noticeService = noticeService;
        this.adManagerService = adManagerService;
        this.navigateProductService = navigateProductService;
        this.categoryService = categoryService;
        this.homepageNavigateService = homepageNavigateService;
        this.productService = productServic;
    }

    @Override
    public HomePageVo findIndex() {
        List<XwBanner> banners = bannerService.list(new QueryWrapper<XwBanner>()
                .select("id,image_url,link_url").eq("show_status", 0));
        List<BannerVo> bannerVos = BeanUtil.copyList(banners, BannerVo.class);
        HomePageVo homePageVo = new HomePageVo();
        homePageVo.setBannerVos(bannerVos);

        List<XwNotice> notices = noticeService.list(new QueryWrapper<XwNotice>().select("id,content").orderByAsc("order_by"));
        List<String> noticeVo = notices.stream().map(XwNotice::getContent).collect(Collectors.toList());
        homePageVo.setNotices(noticeVo);

        List<XwAdManager> position = adManagerService.list(new QueryWrapper<XwAdManager>().select("id,image,url")
                .eq("position", AdPositionType.HOMEPAGE_MAIN.getCode()).eq("show_status", 0).orderByAsc("order_by"));
        AdManagerVo adManagerVo = new AdManagerVo();
        adManagerVo.setPosition(AdPositionType.HOMEPAGE_MAIN.getCode());
        List<Ad> ads = Lists.newArrayListWithCapacity(position.size());
        position.forEach(s -> {
            Ad ad = new Ad();
            ad.setImage(s.getImage());
            ad.setUrl(s.getUrl());
            ads.add(ad);
        });
        adManagerVo.setAds(ads);
        List<AdManagerVo> adManagerVos = Lists.newArrayListWithCapacity(1);
        adManagerVos.add(adManagerVo);
        homePageVo.setAdManagerVos(adManagerVos);

        List<XwCategory> categories = categoryService.list(new QueryWrapper<XwCategory>()
                .select("id,name,sub_name,image_url").eq("show_x", 0).orderByAsc("short_order"));
        List<CategoryVo> categoryVos = BeanUtil.copyList(categories, CategoryVo.class);
        homePageVo.setCategoryVos(categoryVos);

        Integer navigateId = getNavigateId(NavigatePosition.HOT_SALE.getCode());
        NavigateVo hotNavigate = findByNavigateId(navigateId);
        homePageVo.setHotSale(hotNavigate);

        Integer navigateId1 = getNavigateId(NavigatePosition.SHOP_RECOMMEND.getCode());
        NavigateVo shopNavigate = findByNavigateId(navigateId1);
        homePageVo.setShopRecommend(shopNavigate);

        Integer navigateId2 = getNavigateId(NavigatePosition.PARTS.getCode());
        NavigateVo partsNavigate = findByNavigateId(navigateId2);
        homePageVo.setParts(partsNavigate);

        Integer navigateId3 = getNavigateId(NavigatePosition.NEW_PRODUCT.getCode());
        NavigateVo newNavigate = findByNavigateId(navigateId3);
        homePageVo.setNewProducts(newNavigate);

        return homePageVo;
    }

    private Integer getNavigateId(Integer position) {
        XwHomepageNavigate one = homepageNavigateService.getOne(new QueryWrapper<XwHomepageNavigate>()
                .select("id")
                .eq("position", position));
        return one.getId();
    }

    @Override
    public NavigateVo findByNavigateId(Integer id) {

        XwHomepageNavigate homepageNavigate = homepageNavigateService.getOne(new QueryWrapper<XwHomepageNavigate>()
                .select("id,bg_color,bg_image,name")
                .eq("id", id)
        );
        NavigateVo navigateVo = new NavigateVo();
        BeanUtils.copyProperties(homepageNavigate, navigateVo);
        List<XwNavigateProduct> navigateProducts = navigateProductService.list(new QueryWrapper<XwNavigateProduct>()
                .eq("navigate_id", id));

        List<SimpleProductDto> simpleProductDtos = Lists.newArrayListWithCapacity(navigateProducts.size());
        navigateProducts.forEach(i -> {
            XwProduct product = productService.getOne(new QueryWrapper<XwProduct>()
                    .select("id,name,min_price,min_market_price,main_image,label_name,sale_num")
                    .eq("`status`", 0)
                    .eq("id", i.getProductId()));
            if (product != null) {
                SimpleProductDto simpleProductDto = new SimpleProductDto();
                simpleProductDto.setId(product.getId());
                simpleProductDto.setImage(product.getMainImage());
                simpleProductDto.setMarketPrice(product.getMinMarketPrice());
                simpleProductDto.setPrice(product.getMinPrice());
                simpleProductDto.setName(product.getName());
                simpleProductDto.setLabelName(product.getLabelName());
                simpleProductDto.setSaleNum(product.getSaleNum());
                simpleProductDtos.add(simpleProductDto);
            }
        });
        navigateVo.setProductDtos(simpleProductDtos);
        return navigateVo;
    }
}
